<?php
namespace Admin\Controller;

class AdvertController extends AdminController
{
	//广告首页
	public function index ()
	{
		$data = M('advert')->select ();
		if (!$data) {
			$this->assign ('row','暂无搜索内容....');
		}
		$this->assign ('list',$data);
		$this->display();
	}
	//广告添加页
	public function add ()
	{
		$this->display();
	}
	public function insert ()
	{
		//var_dump($_POST);exit;
		$advert = D('advert');

		if (!$advert->create ($_GET)) {
			$this->error($advert->getError());
			exit;
		} else {
			//var_dump($advert->create($_GET)); exit;
			var_dump($advert->add());exit;
			if ($advert->add () > 0) {
				$this->success ('添加成功',U ('Advert/index'));
			} else {
				$this->error ('添加失败');
			}
		}
	}
	//广告编辑页
	public function edit ()
	{
		$id = I('get.id/d');
		$data = M('advert')->field('id,name,iurl,icon')->find($id);
		$this->assign ('list',$data);
		$this->display ();
	}
	public function update ()
	{
		//var_dump($_GET);exit;
		$advert = D ('advert');
		if(!$advert->create ($_GET)) {
			$this->error ($advert->getError);
		} else {
			if($advert->save ()!==false) {
				$this->success ('修改成功',U('Advert/index'));
			} else {
				$this->error ('修改失败');
			}
		}
	}
	public function update1 ()
	{
		//var_dump($_GET);exit;
		/*$advert = D ('advert');
		if (!$advert->create ($_GET)) {
			$this->error ($advert->getError());
		} else {

			if ($advert->save() !== false) {
				if($_GET['status']==1) {
					echo '激活';
				} else {
					echo '禁用';
				}
			} 
		}*/
		$id = I('get.id/d');
		$status = M('advert')->where('id='.$id)->find();
		$status = $status['status'];
		//var_dump($display);exit;
		switch ($status) {
			case '1':
			$data['id'] = $id;
			$data['status'] = 0;
			$advert = D ('advert');
		if (!$advert->create ($data)) {
			$this->error ($advert->getError());
		} else {
			if ($advert->save()!==false) {
				
					echo '禁用';
				}
			
		}
			break;
			case '0':
			$data['id'] = $id;
			$data['status'] = 1;
			$advert = D ('advert');
		if (!$advert->create ($data)) {
			$this->error ($advert->getError());
		} else {
			if ($advert->save()!==false) {
				
					echo '激活';
				}
			
		}
			break;

		}
	}
	//删除广告
	public function delete ()
	{
		$id = I ('get.id/d');
		$list = M('advert')->field('icon')->find($id);
		$file ="./Uploads/".substr($list['icon'],0,8).'/'.substr($list['icon'],8);
		if (M('advert')->delete($id) && unlink ($file)) {
			$this->success ('删除成功');
		} else {
			$this->error ('删除失败');
		}
	}
	public function upload () 
	{
		//var_dump($_POST);exit;
		/*$advert = D('advert');
		if (!$advert->create ($_GET)) {
			$this->error($advert->getError());
			exit;
		}*/
		$upload = new \Think\Upload ();
		$upload->maxSize = 3145728; //设置上传文件的大小
		$upload->exts = array ('jpg','gif','png','jpeg'); //设置文件的上传类型
		$upload->subName = array('date','Ymd'); //设置保存的子目录路径
		$upload->rootPath = './Uploads/'; //上传文件的根路径
		$upload->savePath = ''; //上传文件相对于根路径的保存路径
		$info = $upload->upload ();
		/*var_dump($info); exit;*/
		if (!$info) {
			$this->error($upload->getError());
		} else {
			$icon = date('Ymd').$info['icon']['savename'];
			//var_dump($icon);exit;
			$data['icon'] = $icon;
			$data['name'] = $_POST['name'];
			$data['iurl'] = $_POST['iurl'];
			$advert = D('advert');

		if (!$advert->create ($data)) {
			$this->error($advert->getError());
			exit;
		} else {
			//var_dump($advert->create($_GET)); exit;
			//var_dump($advert->add());exit;
			if ($advert->add () > 0) {
				$this->success ('添加成功',U ('Advert/index'));
			} else {
				$this->error ('添加失败');
			}
		}
			/*var_dump($info); exit;*/
			/*$this->success('上传成功',U('Advert/insert',array('name'=>$_POST['name'],'icon'=>$icon,'iurl'=>$_POST['iurl'])));*/
		}
	}
	public function upload1 () 
	{
		//var_dump($_POST);exit;
		$advert = D('advert');
		if (!$advert->create ($_GET)) {
			$this->error($advert->getError());
			exit;
		}
		$upload = new \Think\Upload ();
		$upload->maxSize = 3145728; //设置上传文件的大小
		$upload->exts = array ('jpg','gif','png','jpeg'); //设置文件的上传类型
		$upload->subName = array('date','Ymd'); //设置保存的子目录路径
		$upload->rootPath = './Uploads/'; //上传文件的根路径
		$upload->savePath = ''; //上传文件相对于根路径的保存路径
		$info = $upload->upload ();
		/*var_dump($info); exit;*/
		if (!$info) {
			$this->error($upload->getError());
		} else {
			$icon = date('Ymd').$info['icon']['savename'];
			//var_dump($icon);exit;
			$data['id'] = $_POST['id'];
			$data['icon'] = $icon;
			$data['name'] = $_POST['name'];
			$data['iurl'] = $_POST['iurl'];
			$advert = D('advert');

		if (!$advert->create ($data)) {
			$this->error($advert->getError());
			exit;
		} else {
			//var_dump($advert->create($_GET)); exit;
			//var_dump($advert->add());exit;
			if ($advert->save () > 0) {
				$this->success ('修改成功',U ('Advert/index'));
			} else {
				$this->error ('修改失败');
			}
		}
		}
	}
}